package com.wisdom.server.register;

import com.wisdom.core.annotation.WisdomRpcService;
import com.wisdom.core.exception.RpcException;
import com.wisdom.core.factory.SpringBeanFactory;
import com.wisdom.core.nacos.NacosServerRegister;
import com.wisdom.server.config.ServerRpcConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @description: <NacosRegister>
 * @author: Leo
 * @date:2021-08-21 19:12
 */
@Component
@DependsOn
public class NacosRegister implements RpcRegister {

    @Autowired
    private NacosServerRegister nacosServerRegister;


    @Autowired
    private ServerRpcConfig serverRpcConfig;

    @Autowired
    private SpringBeanFactory springBeanFactory;


    @Override
    public void serverRegister() {
        //注册服务
        Map<String, Object> annotationClass = SpringBeanFactory.getBeanListByAnnotationClass(WisdomRpcService.class);
        if (!annotationClass.isEmpty()) {
            for (Object bean : annotationClass.values()) {
                //bean上的 WisdomRpcService 注解及其标注的属性
                WisdomRpcService wisdomRpcService = bean.getClass().getAnnotation(WisdomRpcService.class);
                //接口名称
                Class<?> interfaceClass = wisdomRpcService.interfaceClass();
                //在Nacos中注册服务信息  包含 服务提供者的 ip port 接口
                nacosServerRegister.registerInstance(
                        interfaceClass.getName(),
                        serverRpcConfig.getClusterNames(),
                        serverRpcConfig.getServerIp(),
                        serverRpcConfig.getServerPort(),
                        serverRpcConfig.getGroupName(),
                        serverRpcConfig.getWeight());
            }
        }
    }

}
